package com.sanshi.project.assets.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.admin.api.dto.LawsuitDTO;
import com.sanshi.project.admin.api.entity.LawsuitEntity;
import com.sanshi.project.assets.service.LawsuitService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 诉讼申请表
 *
 * @author sunyf
 * @date 2023-11-25 21:39:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lawsuit" )
@Tag(description = "lawsuit" , name = "诉讼申请表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LawsuitController {

    private final  LawsuitService lawsuitService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param lawsuit 诉讼申请表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_view')" )
    public R getLawsuitPage(@ParameterObject Page page, @ParameterObject LawsuitDTO lawsuit) {
        LambdaQueryWrapper<LawsuitEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(LawsuitEntity::getUpdateTime);
        return R.ok(lawsuitService.page(page, wrapper));
    }


    /**
     * 通过id查询诉讼申请表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(lawsuitService.getById(id));
    }

    /**
     * 新增诉讼申请表
     * @param lawsuit 诉讼申请表
     * @return R
     */
    @Operation(summary = "新增诉讼申请表" , description = "新增诉讼申请表" )
    @SysLog("新增诉讼申请表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_add')" )
    public R save(@RequestBody LawsuitEntity lawsuit) {
        return R.ok(lawsuitService.save(lawsuit));
    }

    /**
     * 修改诉讼申请表
     * @param lawsuit 诉讼申请表
     * @return R
     */
    @Operation(summary = "修改诉讼申请表" , description = "修改诉讼申请表" )
    @SysLog("修改诉讼申请表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_edit')" )
    public R updateById(@RequestBody LawsuitEntity lawsuit) {
        return R.ok(lawsuitService.updateById(lawsuit));
    }

    /**
     * 通过id删除诉讼申请表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除诉讼申请表" , description = "通过id删除诉讼申请表" )
    @SysLog("通过id删除诉讼申请表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(lawsuitService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param lawsuit 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('legal_lawsuit_export')" )
    public List<LawsuitEntity> export(LawsuitEntity lawsuit,Long[] ids) {
        return lawsuitService.list(Wrappers.lambdaQuery(lawsuit).in(ArrayUtil.isNotEmpty(ids), LawsuitEntity::getId, ids));
    }
}